import requests
import json
import pymongo
import configparser
import time
import random
from multiprocessing.dummy import Pool


class BiLi(object):
	def __init__(self):
		# 读取配置文件
		self.cf = configparser.ConfigParser()
		self.cf.read('settings.ini')
		host = self.cf.get('DB', 'MONGODB_HOST')
		port = self.cf.getint('DB', 'MONGODB_PORT')
		db_name = self.cf.get('DB', 'MONGODB_DBNAME')
		doc_name = self.cf.get('DB', 'MONGODB_DOCNAME')

		# 连接数据库
		client = pymongo.MongoClient(host=host, port=port)
		self.dbm = client[db_name][doc_name]

	def get_ua(self):
		"""随机返回一个UA"""
		with open('user_agents.txt', 'r') as file:
			uas = []
			for ua in file.readlines():
				uas.append(ua.strip())

			random.shuffle(uas)
			return uas[0]

	def get_info(self, mid):
		"""获取 json 数据并存入数据库"""
		url = 'https://space.bilibili.com/ajax/member/GetInfo'
		headers = {
			'Host': 'space.bilibili.com',
			'User-Agent': self.get_ua(),
			'Accept': 'application/json, text/plain, */*',
			'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
			'Referer': 'https://space.bilibili.com/0/',
			'Content-Type': 'application/x-www-form-urlencoded',
		}
		cookies = {}
		# ip代理池
		proxies = {
			'http': 'http://118.190.95.43:9001',
			'http': 'http://118.190.95.35:9001',
			'http': 'http://61.135.217.7:80',
			'http': 'http://61.135.217.7:80',
			'http': 'http://222.221.11.119:3128',
			'http': 'http://111.160.201.130:30980',
			'http': 'http://61.175.192.2:35420',
			'http': 'http://219.159.38.207:46895',
			'http': 'http://125.40.18.183:62000',
			'http': 'http://180.175.136.195:54584',
			'http': 'http://218.79.58.232:35291',
			'http': 'http://110.86.15.46:58945',
			'http': 'http://113.235.185.160:30586',
		}
		data = 'mid={}&csrf='.format(mid)

		response = requests.post(url=url, headers=headers, data=data, cookies=cookies, proxies=proxies)
		data_js = json.loads(response.text)
		print(data_js)
		# 时间戳转换为时间
		try:
			if 'regtime' in data_js['data'].keys():
				date = time.localtime(data_js['data']['regtime'])
				format_date = time.strftime('%Y-%m-%d %H:%M:%S', date)
			else:
				format_date = ''
		except Exception:
			return

		data = {
			"status": data_js['status'],
			"mid": data_js['data']['mid'],
			"name": data_js['data']['name'],
			"sex": data_js['data']['sex'],
			"rank": data_js['data']['rank'],
			"face": data_js['data']['face'],
			"regtime": format_date,
			"spacesta": data_js['data']['spacesta'],
			"birthday": data_js['data']['birthday'] if 'birthday' in data_js['data'].keys() else '',
			"sign": data_js['data']['sign'],
			"current_level": data_js['data']['level_info']['current_level'],
			"official_verify_type": data_js['data']['official_verify']['type'],
			"official_verify_desc": data_js['data']['official_verify']['desc'],
			"official_verify_suffix": data_js['data']['official_verify']['suffix'],
			"vipType": data_js['data']['vip']['vipType'],
			"vipStatus": data_js['data']['vip']['vipStatus'],
			"toutu": data_js['data']['toutu'],
			"toutuId": data_js['data']['toutuId'],
			"theme": data_js['data']['theme'],
			"theme_preview": data_js['data']['theme_preview'],
			"coins": data_js['data']['coins'],
			"im9_sign": data_js['data']['im9_sign'],
			"fans_badge": data_js['data']['fans_badge'],
		}
		# 存入数据库
		self.dbm.insert(data)


if __name__ == '__main__':
	bili = BiLi()
	pool = Pool(10)

	for i in range(388888888):
		pool.apply_async(bili.get_info, (i, ))

	pool.close()
	pool.join()










